﻿using DivaScriptConv.DivaData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DivaScriptConv
{
    class DexFactory
    {
        private int Offset = 0;
        private DivaData.Header Header;
        private string _path = "";

        public EXP[] DEX;

        public struct EXP
        {
            public int MainOffset;
            public int EyesOffset;
            public int NameOffset;
            public string Name;
            public List<EXPElement> Main;
            public List<EXPElement> Eyes;
        }

        public struct EXPElement
        {
            public double Frame;
            public ushort Both;
            public ushort ID;
            public double Value;
            public double Trans;
        }

        public DexFactory(string filepath)
        {
            _path = filepath;
        }

        public int ReadDEX()
        {
            DivaStream reader = DivaStream.OpenReader(_path);
            Header = new DivaData.Header();

            Header.Format = DivaData.Format.F;
            Header.Signature = reader.ReadInt32();
            if (Header.Signature == 0x43505845)
                Header = reader.ReadHeader(true);
            if (Header.Signature != 0x64)
                return 0;

            Offset = reader.Position - 0x4;
            DEX = new EXP[reader.ReadInt32()];
            int DEXOffset = reader.ReadInt32();
            if (reader.ReadInt32() == 0x00) Header.Format = DivaData.Format.X;

            reader.Seek(DEXOffset + Offset, 0);
            for (int i0 = 0; i0 < DEX.Length; i0++)
                DEX[i0] = new EXP { Main = new List<EXPElement>(), Eyes = new List<EXPElement>() };

            for (int i0 = 0; i0 < DEX.Length; i0++)
            {
                DEX[i0].MainOffset = reader.ReadInt32();
                if (Header.Format == DivaData.Format.X) reader.ReadInt32();
                DEX[i0].EyesOffset = reader.ReadInt32();
                if (Header.Format == DivaData.Format.X) reader.ReadInt32();
            }
            for (int i0 = 0; i0 < DEX.Length; i0++)
            {
                DEX[i0].NameOffset = reader.ReadInt32();
                if (Header.Format == DivaData.Format.X) reader.ReadInt32();
            }

            for (int i0 = 0; i0 < DEX.Length; i0++)
            {
                EXPElement element = new EXPElement();
                reader.Seek(DEX[i0].MainOffset + Offset, 0);
                while (true)
                {
                    element.Frame = reader.ReadSingle();
                    element.Both = reader.ReadUInt16();
                    element.ID = reader.ReadUInt16();
                    element.Value = reader.ReadSingle();
                    element.Trans = reader.ReadSingle();
                    DEX[i0].Main.Add(element);

                    if (element.Frame == 999999 || element.Both == 0xFFFF)
                        break;
                }

                reader.Seek(DEX[i0].EyesOffset + Offset, 0);
                while (true)
                {
                    element.Frame = reader.ReadSingle();
                    element.Both = reader.ReadUInt16();
                    element.ID = reader.ReadUInt16();
                    element.Value = reader.ReadSingle();
                    element.Trans = reader.ReadSingle();
                    DEX[i0].Eyes.Add(element);

                    if (element.Frame == 999999 || element.Both == 0xFFFF)
                        break;
                }

                reader.Seek(DEX[i0].NameOffset + Offset, 0);
                DEX[i0].Name = Encoding.UTF8.GetString(reader.NullTerminated());
            }

            reader.Close();
            return 1;
        }

    }
}
